package club.xyes.pi4j.i2c;

import club.xyes.pi4j.util.ByteUtil;
import com.pi4j.io.i2c.I2CDevice;

import java.io.IOException;

/**
 * 2018/11/6 Create by 郭文梁
 * PrettyIICDevice
 * 装饰I2CDevice类， 提供对Int的写操作
 *
 * @author 郭文梁
 * @date 2018/11/6
 */
public class PrettyIICDevice implements I2CDevice {
    /**
     * 保存地层Device引用
     */
    private I2CDevice device;

    /**
     * 构造时传入
     *
     * @param device 底层设备
     */
    public PrettyIICDevice(I2CDevice device) {
        this.device = device;
    }

    @Override
    public int getAddress() {
        return device.getAddress();
    }

    @Override
    public void write(byte b) throws IOException {
        device.write(b);
    }

    @Override
    public void write(byte[] buffer, int offset, int size) throws IOException {
        device.write(buffer, offset, size);
    }

    @Override
    public void write(byte[] buffer) throws IOException {
        device.write(buffer);
    }

    @Override
    public void write(int address, byte b) throws IOException {
        device.write(address, b);
    }

    @Override
    public void write(int address, byte[] buffer, int offset, int size) throws IOException {
        device.write(address, buffer, offset, size);
    }

    @Override
    public void write(int address, byte[] buffer) throws IOException {
        device.write(address, buffer);
    }

    @Override
    public int read() throws IOException {
        return device.read();
    }

    @Override
    public int read(byte[] buffer, int offset, int size) throws IOException {
        return device.read(buffer, offset, size);
    }

    @Override
    public int read(int address) throws IOException {
        return device.read(address);
    }

    @Override
    public int read(int address, byte[] buffer, int offset, int size) throws IOException {
        return device.read(address, buffer, offset, size);
    }

    @Override
    public int read(byte[] writeBuffer, int writeOffset, int writeSize, byte[] readBuffer, int readOffset, int readSize) throws IOException {
        return device.read(writeBuffer, writeOffset, writeSize, readBuffer, readOffset, readSize);
    }

    /**
     * 写字节
     *
     * @param b unsigned byte = int
     * @throws IOException IO
     */
    public void write(int b) throws IOException {
        byte data = ByteUtil.toUnsignedByte(b);
        device.write(data);
    }

    public void write(int[] buffer, int offset, int size) throws IOException {
        byte[] data = ByteUtil.toUnsignedByte(buffer);
        device.write(data, offset, size);
    }

    public void write(int[] buffer) throws IOException {
        byte[] data = ByteUtil.toUnsignedByte(buffer);
        device.write(data);
    }

    public void write(int address, int data) throws IOException {
        byte b = ByteUtil.toUnsignedByte(data);
        device.write(address, b);
    }

    public void write(int address, int[] buffer, int offset, int size) throws IOException {
        byte[] data = ByteUtil.toUnsignedByte(buffer);
        device.write(address, data, offset, size);
    }

    public void write(int address, int[] buffer) throws IOException {
        byte[] data = ByteUtil.toUnsignedByte(buffer);
        device.write(address, data);
    }
}
